package com.paul.sys.util;


import com.paul.sys.model.Ptstaff;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by yingg_000 on 2015/9/10.
 */
public class MenuFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String reuestURl = request.getRequestURI();



        //表示属于登陆超时拦截
        if(MapUtil.get("outtime")!=null&&(boolean)MapUtil.get("outtime")){
            MapUtil.put("outtime",false);
            response.sendRedirect(request.getContextPath() + "/back/session.do");
            return;
        }

        if (reuestURl.indexOf("back/loginPage.do") != -1 || reuestURl.indexOf("back/login.do") != -1||
                reuestURl.indexOf("back/session.do")!=-1) {

        } else {
            if (reuestURl.indexOf("back/index.do") != -1) {
                Ptstaff user = CommSession.getUser(request);
//                    if(CommSession.getMenu(request)==null){
//
//                    }
//                    if(!CommSession.getMenu(request).contains(reuestURl)){
//                        response.sendRedirect(request.getContextPath()+"/back/error.do");
//                        return;
//                    }
                if (user == null) {
                    response.sendRedirect(request.getContextPath() + "/back/session.do");
                    return;
                }
            }

        }



        chain.doFilter(req, res);
    }

    @Override
    public void destroy() {

    }


}
